package com.cskaoyan.service.wx.order;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.bo.wx.order.OrderCommentBo;
import com.cskaoyan.bean.bo.wx.order.OrderIdBo;
import com.cskaoyan.bean.bo.wx.order.OrderListBo;
import com.cskaoyan.bean.bo.wx.order.OrderSubmitBo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.wx.order.*;
import com.cskaoyan.mapper.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Aaliyah
 * @date 2022/9/9 16:00
 * @description: TODO
 */
@Service
public class WxOrderServiceImpl implements WxOrderService {

    // order表 - order_status
    private static final Short UNPAID = 101;    // 未付款
    private static final Short USER_CANCEL = 102;    // 用户取消
    private static final Short SYSTEM_CANCEL = 103;    // 系统取消
    private static final Short ALREADY_PAID = 201;    // 已付款
    private static final Short APPLY_REFUND = 202;    // 申请退款
    private static final Short ALREADY_REFUND = 203;    // 已退款
    private static final Short ALREADY_SHIP = 301;    // 已发货
    private static final Short USER_RECEIVE = 401;    // 用户收货
    private static final Short SYSTEM_RECEIVE = 402;    // 系统收货

    // 显示在前端的订单状态文本
    private static final String UNPAID_TEXT = "待付款";
    private static final String USER_CANCEL_TEXT = "已取消";    // TODO 还不确定，暂定如此
    private static final String SYSTEM_CANCEL_TEXT = "已取消";    // TODO 还不确定，暂定如此
    private static final String ALREADY_PAID_TEXT = "待发货";
    private static final String APPLY_REFUND_TEXT = "订单取消，退款中";
    private static final String ALREADY_REFUND_TEXT = "已退款";
    private static final String ALREADY_SHIP_TEXT = "待收货";
    private static final String ALREADY_RECEIVE_TEXT = "待评价";
    private static final String SYSTEM_RECEIVE_TEXT = "待收货";
    private static final String ALREADY_COMMENT_TEXT = "已评价";

    // order表 - comment 值为0代表没有待评价商品，其他值代表待评价商品数
    private static final Short NO_GOODS_NEED_TO_BE_COMMENT = 0;


    // order表 - refund_type
    private static final String WECHAT = "微信退款接口";

    // order - aftersale_status 订单售后状态
    private static final Short APPLY_ALLOWED_AFS = 0;
    private static final Short USER_APPLYED_AFS = 1;
    private static final Short ADMIN_APPROVED_AFS = 2;
    private static final Short REFUND_SUCESS_AFS = 3;
    private static final Short ADMIN_REJECT_AFS = 4;
    private static final Short USER_CANCEL_AFS = 5;

    // order_goods表 - comment - 其他值为评论id
    private static final Integer COMMENT_NOT_ALLOWES = -1;  // 超期不可评论
    private static final Integer COMMENT_ALLOWES = 0;   // 可评论

    // comment表 - Type
    private static final Byte GOODS_COMMENT = 0;    // 商品评论
    private static final Byte TOPIC_COMMENT = 1;    // 专题评论

    // comment表 - ValueId
    private static final Integer GOODS_COMMENT_VALUE = 0;   // 商品评论
    private static final Integer TOPIC_COMMENT_VALUE = 0;    // 专题评论

    // coupon_user - status - 优惠券状态
    private static final Short UNUSED = 0;
    private static final Short ALREADY_USED = 1;
    private static final Short EXPIRED = 2;
    private static final Short REMOVED = 3;

    // 订单状态码 - 前端
    private static final Integer WAIT_FOR_PAY = 1;
    private static final Integer WAIT_FOR_SHIP = 2;
    private static final Integer WAIT_FOR_RECEIVE = 3;
    private static final Integer WAIT_FOR_COMMENT = 4;


    // 其他魔法值
    // 数字0
    private static final int ZERO = 0;
    // 默认购买方式代表需要查询确认状态的cart数据条，如果等于其他值，则执行快速购买逻辑
    private static final Integer CART_DEFAULT = 0;
    private static final Short ONE = 1;


    // 组件取出
    @Autowired
    MarketOrderMapper marketOrderMapper;

    @Autowired
    MarketOrderGoodsMapper marketOrderGoodsMapper;

    @Autowired
    MarketCommentMapper marketCommentMapper;

    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Autowired
    MarketCartMapper marketCartMapper;

    @Autowired
    MarketAddressMapper marketAddressMapper;

    @Autowired
    MarketCouponMapper marketCouponMapper;

    @Autowired
    MarketCouponUserMapper marketCouponUserMapper;

    @Autowired
    MarketSystemMapper marketSystemMapper;


    /**
     * @description: 将订单信息存入comment表并修改order_goods表和order表
     * @date: 2022/9/11 17:55
     * @param: [orderCommentBo] 封装了订单商品id和评论信息
     */
    @Override
    public void orderComment(OrderCommentBo orderCommentBo) {

        // 更新order表
        // 获取Order表id
        MarketOrderGoods orderGoods = marketOrderGoodsMapper.selectByPrimaryKey(orderCommentBo.getOrderGoodsId());
        Integer orderId = orderGoods.getOrderId();
        // 获取目标订单数据
        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(orderId);
        // 构造新订单数据
        marketOrder.setComments((short) (marketOrder.getComments() - 1));
        marketOrder.setUpdateTime(getNow());
        // 更新order表
        marketOrderMapper.updateByPrimaryKeySelective(marketOrder);

        // 将评论信息存入comment表中
        MarketComment marketComment = new MarketComment();
        marketComment.setValueId(orderGoods.getGoodsId());
        marketComment.setType(GOODS_COMMENT);
        marketComment.setContent(orderCommentBo.getContent());
        marketComment.setUserId(marketOrder.getUserId());
        marketComment.setHasPicture(orderCommentBo.getHasPicture());
        marketComment.setPicUrls(orderCommentBo.getPicUrls());
        marketComment.setStar(orderCommentBo.getStar());
        marketComment.setAddTime(getNow());
        marketComment.setUpdateTime(getNow());
        marketCommentMapper.insert(marketComment);

        // 更新order_goods表
        orderGoods.setComment(marketComment.getId());
        orderGoods.setUpdateTime(getNow());
        marketOrderGoodsMapper.updateByPrimaryKeySelective(orderGoods);

    }


    /**
     * @description: 将符合状态要求的订单数据查询出来，并封装VO返回
     * @date: 2022/9/9 16:02
     * @param: [orderListBo] 封装了分页信息和订单状态信息
     */
    @SneakyThrows
    @Override
    public CommonData orderList(OrderListBo orderListBo) {

        Integer showType = orderListBo.getShowType();

        // 根据showType和userId设置查询Example
        MarketOrderExample orderExample = getMarketOrderExampleByUserIdAndShowType(getUserId(), showType);

        // 进行分页查询
        PageHelper.startPage(orderListBo.getPage(), orderListBo.getLimit());
        List<MarketOrder> marketOrders = marketOrderMapper.selectByExample(orderExample);
        PageInfo<MarketOrder> pageInfo = new PageInfo<>(marketOrders);


        // 将结果封装进VO list 中
        List<OrderListSingleVo> list = new ArrayList<>();
        for (MarketOrder po : marketOrders) {
            OrderListSingleVo vo = new OrderListSingleVo();
            vo.setActualPrice(po.getActualPrice());
            vo.setAftersaleStatus(po.getAftersaleStatus());
            if (po.getGrouponPrice() != null) {
                vo.setIsGroupin(true);
            } else {
                vo.setIsGroupin(false);
            }
            vo.setId(po.getId());
            vo.setOrderSn(po.getOrderSn());
            vo.setOrderStatusText(getStatusTestByPoStatusAndComments(po.getOrderStatus(), po.getComments()));

            // 获取goodsList
            // 根据订单id查找出该订单的商品
            MarketOrderGoodsExample orderGoodsExample = new MarketOrderGoodsExample();
            MarketOrderGoodsExample.Criteria orderGoodsExampleCriteria = orderGoodsExample.createCriteria();
            orderGoodsExample.setDistinct(true);
            orderGoodsExampleCriteria.andDeletedEqualTo(false);
            orderGoodsExampleCriteria.andOrderIdEqualTo(po.getId());
            List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(orderGoodsExample);
            // 将需要的数据装入goodsList
            List<GoodsListInfoOfOrderListSingleVo> goodsList = new ArrayList<>();
            for (MarketOrderGoods goods : marketOrderGoods) {
                GoodsListInfoOfOrderListSingleVo elem = new GoodsListInfoOfOrderListSingleVo();
                elem.setGoodsName(goods.getGoodsName());
                elem.setId(goods.getId());
                elem.setNumber(goods.getNumber());
                elem.setPicUrl(goods.getPicUrl());
                elem.setPrice(goods.getPrice());
                elem.setSpecifications(goods.getSpecifications());
                goodsList.add(elem);
            }

            // 将goodsList封装进VO
            vo.setGoodsList(goodsList);

            // 获取HandleOption并封装进vo中
            vo.setHandleOption(HandleOption.setDataByOrderStatus(po.getOrderStatus()));

            // 将封装好的VO装入orderListSingleVos中
            list.add(vo);
        }

        // 将结果封装进CommonData
        CommonData data = CommonData.data(pageInfo);
        data.setList(list);

        return data;
    }

    private MarketOrderExample getMarketOrderExampleByUserIdAndShowType(Integer tempUserId, Integer showType) {
        MarketOrderExample orderExample = new MarketOrderExample();
        MarketOrderExample.Criteria orderExampleCriteria = orderExample.createCriteria();
        orderExample.setDistinct(true);
        orderExampleCriteria.andUserIdEqualTo(tempUserId);
        orderExampleCriteria.andDeletedEqualTo(false);
        // 判断showType参数，确定筛选条件
        if (showType.equals(WAIT_FOR_PAY)) {
            orderExampleCriteria.andOrderStatusEqualTo(UNPAID);
        } else if (showType.equals(WAIT_FOR_SHIP)) {
            orderExampleCriteria.andOrderStatusEqualTo(ALREADY_PAID);
        } else if (showType.equals(WAIT_FOR_RECEIVE)) {
            ArrayList<Short> orderStatus = new ArrayList<>();
            orderStatus.add(ALREADY_SHIP);
            orderStatus.add(SYSTEM_RECEIVE);
            orderExampleCriteria.andOrderStatusIn(orderStatus);
        } else if (showType.equals(WAIT_FOR_COMMENT)) {
            orderExampleCriteria.andOrderStatusEqualTo(USER_RECEIVE);
            orderExampleCriteria.andCommentsGreaterThan(NO_GOODS_NEED_TO_BE_COMMENT);
        }
        return orderExample;
    }

    /**
     * @description: 根据id将前端需要的信息查询出来，并封装VO返回
     * @date: 2022/9/9 22:50
     * @param: [orderId] 订单id
     */
    @Override
    public OrderDetailVo orderDetail(int orderId) {

        // 获取订单基本信息，封装进OrderInfoOfOrderDetailVo
        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(orderId);
        OrderInfoOfOrderDetailVo orderInfo = creatOrderInfoVoByOrder(marketOrder);

        // 获取订单商品信息，封装进List<OrderGoodsOfOrderDetailVo>
        // 获取信息
        MarketOrderGoodsExample orderGoodsExample = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria criteria = orderGoodsExample.createCriteria();
        orderGoodsExample.setDistinct(true);
        criteria.andOrderIdEqualTo(marketOrder.getId());
        List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(orderGoodsExample);
        // 信息封装
        ArrayList<OrderGoodsOfOrderDetailVo> orderGoods = putDataInVoList(marketOrderGoods);

        // TODO 还有一个参数 expressInfo  暂时不知道有什么用

        // 将获取到的信息封装进VO
        OrderDetailVo orderDetailVo = new OrderDetailVo();
        orderDetailVo.setOrderInfo(orderInfo);
        orderDetailVo.setOrderGoods(orderGoods);

        return orderDetailVo;
    }

    private ArrayList<OrderGoodsOfOrderDetailVo> putDataInVoList(List<MarketOrderGoods> marketOrderGoods) {
        ArrayList<OrderGoodsOfOrderDetailVo> orderGoods = new ArrayList<>();
        for (MarketOrderGoods po : marketOrderGoods) {
            OrderGoodsOfOrderDetailVo vo = new OrderGoodsOfOrderDetailVo();
            vo.setAddTime(po.getAddTime());
            vo.setComment(po.getComment());
            vo.setDeleted(po.getDeleted());
            vo.setGoodsId(po.getGoodsId());
            vo.setGoodsName(po.getGoodsName());
            vo.setGoodsSn(po.getGoodsSn());
            vo.setId(po.getId());
            vo.setNumber(po.getNumber());
            vo.setOrderId(po.getOrderId());
            vo.setPicUrl(po.getPicUrl());
            vo.setPrice(po.getPrice());
            vo.setProductId(po.getProductId());
            vo.setSpecifications(po.getSpecifications());
            vo.setUpdateTime(po.getAddTime());
            orderGoods.add(vo);
        }
        return orderGoods;
    }

    private OrderInfoOfOrderDetailVo creatOrderInfoVoByOrder(MarketOrder marketOrder) {
        OrderInfoOfOrderDetailVo orderInfo = new OrderInfoOfOrderDetailVo();
        orderInfo.setActualPrice(marketOrder.getActualPrice());
        orderInfo.setAddTime(marketOrder.getAddTime());
        orderInfo.setAddress(marketOrder.getAddress());
        orderInfo.setAftersaleStatus(marketOrder.getAftersaleStatus());
        orderInfo.setConsignee(marketOrder.getConsignee());
        orderInfo.setCouponPrice(marketOrder.getCouponPrice());
        orderInfo.setExpCode(marketOrder.getShipChannel());
        orderInfo.setExpName(marketOrder.getShipChannel());
        orderInfo.setExpNo(marketOrder.getShipSn());
        orderInfo.setFreightPrice(marketOrder.getFreightPrice());
        orderInfo.setGoodsPrice(marketOrder.getGoodsPrice());
        orderInfo.setHandleOption(HandleOption.setDataByOrderStatus(marketOrder.getOrderStatus()));
        orderInfo.setId(marketOrder.getId());
        orderInfo.setMessage(marketOrder.getMessage());
        orderInfo.setMobile(marketOrder.getMobile());
        orderInfo.setOrderSn(marketOrder.getOrderSn());
        orderInfo.setOrderStatusText(getStatusTestByPoStatusAndComments(marketOrder.getOrderStatus(), marketOrder.getComments()));
        return orderInfo;
    }

    /**
     * @description: 根据传入的订单状态，返回供前端显示的订单状态信息
     * @date: 2022/9/9 23:35
     * @param: [orderStatus] 订单状态
     */
    private String getStatusTestByPoStatusAndComments(Short orderStatus, Short marketOrderComments) {
        if (orderStatus.equals(UNPAID)) {
            return UNPAID_TEXT;
        } else if (orderStatus.equals(USER_CANCEL)) {
            return USER_CANCEL_TEXT;
        } else if (orderStatus.equals(SYSTEM_CANCEL)) {
            return SYSTEM_CANCEL_TEXT;
        } else if (orderStatus.equals(ALREADY_PAID)) {
            return ALREADY_PAID_TEXT;
        } else if (orderStatus.equals(APPLY_REFUND)) {
            return APPLY_REFUND_TEXT;
        } else if (orderStatus.equals(ALREADY_REFUND)) {
            return ALREADY_REFUND_TEXT;
        } else if (orderStatus.equals(ALREADY_SHIP)) {
            return ALREADY_SHIP_TEXT;
        } else if (orderStatus.equals(USER_RECEIVE)) {
            if (marketOrderComments.equals(NO_GOODS_NEED_TO_BE_COMMENT)) {
                return ALREADY_COMMENT_TEXT;
            }
            return ALREADY_RECEIVE_TEXT;
        } else if (orderStatus.equals(SYSTEM_RECEIVE)) {
            return SYSTEM_RECEIVE_TEXT;
        }
        // TODO  需要增加参数校验 or 异常管理
        return null;
    }

    /**
     * @description: 根据id对订单进行逻辑删除
     * @date: 2022/9/11 16:23
     * @param: [orderIdBo] 封装了订单id
     */
    @Override
    public void orderDelete(OrderIdBo orderIdBo) {
        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(orderIdBo.getOrderId());
        marketOrder.setDeleted(true);
        marketOrder.setUpdateTime(getNow());
        marketOrderMapper.updateByPrimaryKeySelective(marketOrder);
    }

    /**
     * @description: 修改Order表的订单状态信息和更新时间
     * @date: 2022/9/11 19:25
     * @param: [orderIdBo]
     */
    @Override
    public void orderConfirm(OrderIdBo orderIdBo) {
        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(orderIdBo.getOrderId());
        marketOrder.setOrderStatus(USER_RECEIVE);
        marketOrder.setUpdateTime(getNow());
        marketOrderMapper.updateByPrimaryKeySelective(marketOrder);
    }

    /**
     * @description: 修改order表的订单状态和更新时间，加入退货信息
     * @date: 2022/9/11 19:29
     * @param: [orderIdBo] 封装了订单id
     */
    @Override
    public void orderRefund(OrderIdBo orderIdBo) {
        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(orderIdBo.getOrderId());
        marketOrder.setOrderStatus(APPLY_REFUND);
        marketOrder.setAftersaleStatus(USER_APPLYED_AFS);
        // 退款金额为实付款-运费
        marketOrder.setRefundAmount(marketOrder.getActualPrice().subtract(marketOrder.getFreightPrice()));
        marketOrder.setRefundType(WECHAT);
        marketOrder.setUpdateTime(getNow());
        marketOrderMapper.updateByPrimaryKey(marketOrder);
    }

    /**
     * @description: 回显订单的商品信息
     * @date: 2022/9/11 20:35
     * @param: [orderId, goodsId] 订单id 商品id
     */
    @Override
    public MarketOrderGoods orderGoods(Integer orderId, Integer goodsId) {
        MarketOrderGoodsExample marketOrderGoodsExample = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria criteria = marketOrderGoodsExample.createCriteria();
        criteria.andOrderIdEqualTo(orderId);
        criteria.andGoodsIdEqualTo(goodsId);
        criteria.andDeletedEqualTo(false);
        List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(marketOrderGoodsExample);
        return marketOrderGoods.get(0);
    }

    /**
     * @description:  往order表和order_goods表插入新数据，并更新cart表
     * @date: 2022/9/12 9:19
     * @param: [orderSubmitBo] 封装了购物车、购物方式和地址相关的信息
     */
    @Override
    public OrderSubmitVo orderSubmit(OrderSubmitBo orderSubmitBo) {

        // order表 - 包邮门槛和邮费
        final BigDecimal FREE_EXP = getFREE_EXP();
        final BigDecimal EXP_FEE = getEXP_FEE();


        // 查询购物车
        // 先找到一条购物车信息，用以获取用户id
        Integer userId = getUserId();

        // 查找出该用户所有下单商品的信息
        MarketCartExample marketCartExample = new MarketCartExample();
        MarketCartExample.Criteria criteria = marketCartExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andCheckedEqualTo(true);
        criteria.andDeletedEqualTo(false);
        List<MarketCart> marketCarts = marketCartMapper.selectByExample(marketCartExample);


        // 创建一条新的order数据
        MarketOrder marketOrder = new MarketOrder();

        // 地址信息
        MarketAddress marketAddress = marketAddressMapper.selectByPrimaryKey(orderSubmitBo.getAddressId());
        marketOrder.setConsignee(marketAddress.getName());
        marketOrder.setMobile(marketAddress.getTel());
        marketOrder.setAddress(marketAddress.getAddressDetail());


        // 商品费用逻辑
        BigDecimal goodsPrice = BigDecimal.ZERO;
        for (MarketCart cart : marketCarts) {
            goodsPrice = goodsPrice.add(cart.getPrice().multiply(BigDecimal.valueOf(cart.getNumber())));
        }
        marketOrder.setGoodsPrice(goodsPrice);
        // 邮费逻辑
        if (marketOrder.getGoodsPrice().compareTo(FREE_EXP) < ZERO) {
            // 不包邮
            marketOrder.setFreightPrice(EXP_FEE);
        }
        // 优惠券使用逻辑，用以计算费用，销券
        MarketCoupon marketCoupon = marketCouponMapper.selectByPrimaryKey(orderSubmitBo.getCouponId());
        if (marketCoupon!=null){
            // 使用优惠券的情况
            marketOrder.setCouponPrice(marketCoupon.getDiscount());
            // 销券
            MarketCouponUser marketCouponUser = marketCouponUserMapper.selectByPrimaryKey(orderSubmitBo.getUserCouponId());
            marketCouponUser.setStatus(ALREADY_USED);
            marketCouponUser.setUsedTime(getNow());
            marketCouponUser.setOrderId(marketOrder.getId());
            marketCouponUser.setUpdateTime(getNow());
        }

        marketOrder.setIntegralPrice(BigDecimal.ZERO);  // TODO 积分相关的逻辑
        marketOrder.setGrouponPrice(BigDecimal.ZERO);   // TODO 团购相关的逻辑

        // 订单费用=商品费用+运费-优惠券
        marketOrder.setOrderPrice(marketOrder.getGoodsPrice().add(marketOrder.getFreightPrice()).subtract(marketOrder.getCouponPrice()));
        marketOrder.setActualPrice(marketOrder.getOrderPrice().subtract(marketOrder.getIntegralPrice()));


        // 生成订单编号
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String orderSn = format.format(getNow());
        marketOrder.setOrderSn(orderSn);


        // 设置订单、售后状态、待评价商品数、备注及时间信息
        marketOrder.setUserId(getUserId());
        marketOrder.setOrderStatus(UNPAID);
        marketOrder.setAftersaleStatus(APPLY_ALLOWED_AFS);
        marketOrder.setComments((short) marketCarts.size());
        marketOrder.setMessage(orderSubmitBo.getMessage());
        marketOrder.setAddTime(getNow());
        marketOrder.setUpdateTime(getNow());


        // 将新数据插入order表
        marketOrderMapper.insert(marketOrder);


        // 向order_goods表插入新数据
        for (MarketCart cart : marketCarts) {
            MarketOrderGoods orderGoods = new MarketOrderGoods();

            orderGoods.setOrderId(marketOrder.getId());
            orderGoods.setGoodsId(cart.getGoodsId());
            orderGoods.setGoodsName(cart.getGoodsName());
            orderGoods.setGoodsSn(cart.getGoodsSn());
            orderGoods.setProductId(cart.getProductId());
            orderGoods.setNumber(cart.getNumber());
            orderGoods.setPrice(cart.getPrice());
            orderGoods.setSpecifications(cart.getSpecifications());
            orderGoods.setPicUrl(cart.getPicUrl());
            orderGoods.setComment(COMMENT_ALLOWES);
            orderGoods.setAddTime(getNow());
            orderGoods.setUpdateTime(getNow());
            orderGoods.setDeleted(false);

            marketOrderGoodsMapper.insert(orderGoods);
        }


        // 对购物车已购买的商品进行逻辑删除
        for (int i = 0; i < marketCarts.size(); i++) {
            marketCarts.get(i).setDeleted(true);
            marketCarts.get(i).setUpdateTime(getNow());
            marketCartMapper.updateByPrimaryKeySelective(marketCarts.get(i));
        }

        // 将需要的数据封装进VO
        OrderSubmitVo orderSubmitVo = new OrderSubmitVo();
        orderSubmitVo.setGrouponLinkId(orderSubmitBo.getGrouponLinkId());
        orderSubmitVo.setOrderId(marketOrder.getId());

        return orderSubmitVo;
    }

    /**
     * @description: 预付款功能，更新订单表的状态和更新时间
     * @date: 2022/9/12 9:23
     * @param: [orderIdBo] 订单id
     */
    @Override
    public void orderPrepay(OrderIdBo orderIdBo) {
        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(orderIdBo.getOrderId());

        // 生成付款编号
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String payId = format.format(getNow());
        marketOrder.setPayId(payId);

        // 修改了订单状态和时间信息
        marketOrder.setOrderStatus(ALREADY_PAID);
        marketOrder.setPayTime(getNow());
        marketOrder.setUpdateTime(getNow());

        marketOrderMapper.updateByPrimaryKeySelective(marketOrder);
    }

    /**
     * @description: 取消订单功能，更新订单表的状态和更新时间
     * @date: 2022/9/12 9:23
     * @param: [orderIdBo]
     */
    @Override
    public void orderCancel(OrderIdBo orderIdBo) {
        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(orderIdBo.getOrderId());
        marketOrder.setOrderStatus(USER_CANCEL);
        marketOrder.setUpdateTime(getNow());
        marketOrderMapper.updateByPrimaryKeySelective(marketOrder);
    }

    /**
     * @description: 获取当前Date
     * @date: 2022/9/11 18:42
     * @param: []
     */
    private Date getNow() {
        return new Date(System.currentTimeMillis());
    }

    private static Integer getUserId() {
        Subject subject = SecurityUtils.getSubject();
        MarketUser principal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        //拿到当前用户信息的id
        Integer id = principal.getId();
        return id;
    }

    private BigDecimal getFREE_EXP(){
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();
        criteria.andKeyNameEqualTo("market_express_freight_min");
        List<MarketSystem> marketSystems = marketSystemMapper.selectByExample(marketSystemExample);
        MarketSystem marketSystem = marketSystems.get(0);
        String keyValue = marketSystem.getKeyValue();
        BigDecimal bigDecimal = BigDecimal.valueOf(Double.parseDouble(keyValue));
        return bigDecimal;
    }

    private BigDecimal getEXP_FEE(){
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();
        criteria.andKeyNameEqualTo("market_express_freight_value");
        List<MarketSystem> marketSystems = marketSystemMapper.selectByExample(marketSystemExample);
        MarketSystem marketSystem = marketSystems.get(0);
        String keyValue = marketSystem.getKeyValue();
        BigDecimal bigDecimal = BigDecimal.valueOf(Double.parseDouble(keyValue));
        return bigDecimal;
    }
}
